package com.darkpegasus.trillian.parser;

import com.darkpegasus.util.StringUtil;
import com.darkpegasus.util.DateUtil;

import java.util.*;

/**
 * Conversations are immutable
 *
 * @author alek
 */
public class Conversation {
    private final Collection<AbstractTrillianEvent> events;
    private final Set<String> participants;
    private final Date startTime;
    private final Date endTime;
    private final String location;

    public Conversation(Collection<AbstractTrillianEvent> events){
        Set<String> participants = new HashSet<String>();

        Iterator<AbstractTrillianEvent> eventIter = events.iterator();

        AbstractTrillianEvent event;
        Date startTime = null;
        Date endTime = null;
        String location = null;

        if(eventIter.hasNext()){
            event = eventIter.next();

            startTime = event.getTimestamp();
            endTime = event.getTimestamp();
            participants.add(event.getSenderId());
            location = event.getLocation();
        }

        while(eventIter.hasNext()){
            event = eventIter.next();

            participants.add(event.getSenderId());
            endTime = event.getTimestamp();
            if(!StringUtil.isEmptyOrNull(event.getLocation())){
                location = event.getLocation();
            }
        }

        this.events = Collections.unmodifiableList(new ArrayList<AbstractTrillianEvent>(events));
        this.participants = Collections.unmodifiableSet(participants);
        this.startTime = startTime;
        this.endTime = endTime;
        this.location = location;
    }

    public Set<String> getParticipants() {
        return participants;
    }

    public Date getStartTime() {
        return DateUtil.copyDate(startTime);
    }

    public Date getEndTime() {
        return DateUtil.copyDate(endTime);
    }

    public String getLocation() {
        return location;
    }
}
